package org.example.springweibo.controller;

import org.example.springweibo.jsonresult.JsonResult;
import org.example.springweibo.pojo.dto.CommentDTO;
import org.example.springweibo.pojo.entity.Comment;
import org.example.springweibo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/v1/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping("/new-comment")
    public JsonResult addComment(CommentDTO comment) {
        return JsonResult.ok(commentService.addComment(commentResult(comment)));
    }

    @DeleteMapping("/delete-comment")
    public JsonResult deleteComment(CommentDTO comment){
        return JsonResult.ok(commentService.deleteComment(commentResult(comment)));
    }

    @GetMapping("/select-comment")
    public JsonResult selectMyComment(CommentDTO commentDTO){
        return JsonResult.ok(commentService.selectCommentByUser(commentDTO));
    }

    private Comment commentResult(CommentDTO commentDTO){
        Comment comment = new Comment();
        comment.setContent(commentDTO.getContent());
        comment.setId(commentDTO.getId());
        comment.setWeiboId(commentDTO.getWeiboId());
        comment.setUserId(commentDTO.getUserId());
        return comment;
    }
}
